package com.css.fxfzypg.util;

import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lixiaofeng on 2020/4/17.
 */

public class PlatformRSAUtils {
    public static final String PUBLIC_KEY = "publicKey";
    public static final String PRIVATE_KEY = "privateKey";
    private static final String STRING_CHARSET = "UTF-8";
    private static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
    private static final String RSA_TRANSFORMATION = "RSA";
    private static final int MAX_ENCRYPT_BLOCK = 117;
    private static final int MAX_DECRYPT_BLOCK = 128;

    public PlatformRSAUtils() {
    }

    public static Map<String, String> createKeys() throws Exception {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA_TRANSFORMATION);
        kpg.initialize(1024);
        KeyPair keyPair = kpg.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        String publicKeyStr = PlatformBase64Utils.encodeToString(publicKey.getEncoded(), false);
        PrivateKey privateKey = keyPair.getPrivate();
        String privateKeyStr = PlatformBase64Utils.encodeToString(privateKey.getEncoded(), false);
        HashMap keyMap = new HashMap();
        keyMap.put(PUBLIC_KEY, publicKeyStr);
        keyMap.put(PRIVATE_KEY, privateKeyStr);
        return keyMap;
    }

    public static String encrypt(String content, String publicKeyStr) throws Exception {
        RSAPublicKey publicKey = getPublicKey(publicKeyStr);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(1, publicKey);
        byte[] var4 = encryptBytes(content.getBytes(STRING_CHARSET), cipher);
        return PlatformBase64Utils.encodeToString(var4, false);
    }

    public static String decrypt(String content, String privateKeyStr) throws Exception {
        RSAPrivateKey privateKey = getPrivateKey(privateKeyStr);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(2, privateKey);
        byte[] var4 = decryptBytes(PlatformBase64Utils.decode(content), cipher);
        return new String(var4, STRING_CHARSET);
    }

    public static String sign(String content, String privateKeyStr) throws Exception {
        RSAPrivateKey privateKey = getPrivateKey(privateKeyStr);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(1, privateKey);
        byte[] var4 = encryptBytes(content.getBytes(STRING_CHARSET), cipher);
        return PlatformBase64Utils.encodeToString(var4, false);
    }

    public static String verify(String content, String publicKeyStr) throws Exception {
        RSAPublicKey publicKey = getPublicKey(publicKeyStr);
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(2, publicKey);
        byte[] var4 = decryptBytes(PlatformBase64Utils.decode(content), cipher);
        return new String(var4, STRING_CHARSET);
    }

    private static RSAPublicKey getPublicKey(String publicKeyStr) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_TRANSFORMATION);
        X509EncodedKeySpec var2 = new X509EncodedKeySpec(PlatformBase64Utils.decode(publicKeyStr));
        RSAPublicKey var3 = (RSAPublicKey)keyFactory.generatePublic(var2);
        return var3;
    }

    private static RSAPrivateKey getPrivateKey(String privateKeyStr) throws Exception {
        KeyFactory keyFactory = KeyFactory.getInstance(RSA_TRANSFORMATION);
        PKCS8EncodedKeySpec var2 = new PKCS8EncodedKeySpec(PlatformBase64Utils.decode(privateKeyStr));
        RSAPrivateKey var3 = (RSAPrivateKey)keyFactory.generatePrivate(var2);
        return var3;
    }

    private static byte[] encryptBytes(byte[] data, Cipher cipher) throws Exception {
        int inputLen = data.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;

        for(int i = 0; inputLen - offSet > 0; offSet = i * MAX_ENCRYPT_BLOCK) {
            byte[] cache;
            if (inputLen - offSet > MAX_ENCRYPT_BLOCK) {
                cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(data, offSet, inputLen - offSet);
            }

            out.write(cache, 0, cache.length);
            ++i;
        }

        byte[] encryptedData = out.toByteArray();
        out.close();
        return encryptedData;
    }

    private static byte[] decryptBytes(byte[] encryptedData, Cipher cipher) throws Exception {
        int inputLen = encryptedData.length;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int offSet = 0;

        for(int i = 0; inputLen - offSet > 0; offSet = i * MAX_DECRYPT_BLOCK) {
            byte[] cache;
            if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
                cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
            } else {
                cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
            }

            out.write(cache, 0, cache.length);
            ++i;
        }

        byte[] decryptedData = out.toByteArray();
        out.close();
        return decryptedData;
    }

}
